package com.meilingcloud.biobank.controller;

import com.meilingcloud.biobank.domain.SampleType;
import com.meilingcloud.biobank.service.ISampleTypeService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 样本类型 Controller
 *
 * @author chenl
 * @date 2021-03-04
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/sampleType")
public class SampleTypeController extends BaseController {

    private final ISampleTypeService iSampleTypeService;

    /**
     * 查询样本类型 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleType:list')")
    @GetMapping("/list")
    public AjaxResult list(SampleType sampleType) {
        List<SampleType> list = iSampleTypeService.queryList(sampleType);
        return AjaxResult.success(list);
    }

    /**
     * 导出样本类型 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleType:export')")
    @Log(title = "样本类型 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SampleType sampleType) {
        List<SampleType> list = iSampleTypeService.queryList(sampleType);
        ExcelUtil<SampleType> util = new ExcelUtil<SampleType>(SampleType.class);
        return util.exportExcel(list, "sampleType");
    }

    /**
     * 获取样本类型 详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleType:query')")
    @GetMapping(value = "/{sampTypeId}")
    public AjaxResult getInfo(@PathVariable("sampTypeId") Long sampTypeId) {
        return AjaxResult.success(iSampleTypeService.queryById(sampTypeId));
    }

    /**
     * 新增样本类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleType:add')")
    @Log(title = "样本类型 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SampleType sampleType) {
        sampleType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iSampleTypeService.save(sampleType));
    }

    /**
     * 修改样本类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleType:edit')")
    @Log(title = "样本类型 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody SampleType sampleType) {
        sampleType.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSampleTypeService.updateSampTypeById(sampleType));
    }

    /**
     * 删除样本类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleType:remove')")
    @Log(title = "样本类型 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sampTypeIds}")
    public AjaxResult remove(@PathVariable Long[] sampTypeIds) {
        return toAjax(iSampleTypeService.removeSampTypeByIds(Arrays.asList(sampTypeIds)));
    }
}
